package TextNor.Initial;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

/**
 *
 * @author phamthanh
 */
public final class DetectNSW {

    private HashSet dic = new HashSet();
    public ArrayList<String> greekAr = new ArrayList<String>();

    public DetectNSW(String inF, String ouF) throws XMLStreamException, FileNotFoundException, IOException {
        long startTime = System.currentTimeMillis();

        setDic();
        setGreek();
        int nswNumber = 0;
        int greekNumber = 0;

        System.out.println("package TextNor.Initial;class DetectNSW");

        FileInputStream fis = new FileInputStream(inF);
        XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(fis);

        FileOutputStream fos = new FileOutputStream(ouF);
        OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF8");
        XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(osw);

        xmlStreamWriter.writeStartElement("root");
        while (xmlStreamReader.hasNext()) {
            int eventCode = xmlStreamReader.next();
            switch (eventCode) {
                case XMLStreamConstants.START_ELEMENT:
                    String localName = xmlStreamReader.getLocalName();
                    if (localName.equals("record")) {
                        String RecId = xmlStreamReader.getAttributeValue(0);
                        xmlStreamWriter.writeStartElement(localName);
                        xmlStreamWriter.writeAttribute("RecId", RecId);
                    }
                    if (localName.equals("URL") || localName.equals("MONEY") || localName.equals("SEQN")) {
                        String url = xmlStreamReader.getElementText();
                        xmlStreamWriter.writeStartElement(localName);
                        xmlStreamWriter.writeCharacters(url);
                        xmlStreamWriter.writeEndElement();
                    }
                    if (localName.equals("Infomation")) {
                        xmlStreamWriter.writeStartElement(localName);
                        String news = xmlStreamReader.getElementText();
                        StringTokenizer st = new StringTokenizer(news);
                        while (st.hasMoreTokens()) {
                            String token = st.nextToken();
                            String tokenlw = token.toLowerCase();
                            if (isGreekNumber(token)) {
                                xmlStreamWriter.writeStartElement("W");
                                xmlStreamWriter.writeCharacters(token);
                                xmlStreamWriter.writeEndElement();
                                greekNumber++;
                                nswNumber++;
                                System.out.println("            +" + greekNumber + ". " + token);
                            } else {
                                if (isPunc(token) || dic.contains(tokenlw)) {
                                    xmlStreamWriter.writeCharacters(token + " ");
                                } else {
                                    xmlStreamWriter.writeStartElement("W");
                                    xmlStreamWriter.writeCharacters(token);
                                    xmlStreamWriter.writeEndElement();
                                    nswNumber++;
                                }
                            }
                        }
                        xmlStreamWriter.writeEndElement();
                    }
                    break;
                case XMLStreamConstants.END_ELEMENT:
                    String endLocalName = xmlStreamReader.getLocalName();
                    if (endLocalName.equals("record")) {
                        xmlStreamWriter.writeEndElement();

                    }
                    break;
                case XMLStreamConstants.CHARACTERS:
                    String character = xmlStreamReader.getText();
                    break;

                default:
                    break;
            }
        }

        xmlStreamWriter.writeEndElement();
        xmlStreamReader.close();
        xmlStreamWriter.close();
        fis.close();
        fos.close();

        long endTime = System.currentTimeMillis();
        long time = endTime - startTime;
        System.out.println("Thoi gian phat hien NSW la: " + time);
        System.out.println("        - So nsw: " + nswNumber);
        System.out.println("        - So greeknumber: " + greekNumber);

    }

    public void setDic() throws UnsupportedEncodingException, FileNotFoundException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("resources\\amtiet.txt"), "UTF16"));
        String amtiet;
        //   int i = 0;
        while ((amtiet = br.readLine()) != null) {
            dic.add(amtiet);
//            i++;
        }
//        System.out.println(i);
    }

    public boolean isGreekNumber(String nsw) {
        if (greekAr.contains(nsw)) {
            return true;
        }
        return false;
    }

    public void setGreek() throws UnsupportedEncodingException, FileNotFoundException, IOException {
//        greekAr.add("I");
//        greekAr.add("II");
//        greekAr.add("III");
//        greekAr.add("IV");
//        greekAr.add("V");
//        greekAr.add("VI");
//        greekAr.add("VII");
//        greekAr.add("VIII");
//        greekAr.add("IX");
//        greekAr.add("X");
//        greekAr.add("XI");
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("resources\\greekNumber.db"), "UTF8"));
        String greekNumber;
        int i = 0;
        while ((greekNumber = br.readLine()) != null) {
            greekAr.add(greekNumber);
//            i++;
        }
//        System.out.println(i);
    }

    public boolean isPunc(String str) {
        // Pattern p=Pattern.compile("(\\[)|(\\])|(\\()|(\\))|(\")|(\')|(...)");
        Pattern p = Pattern.compile("(^\\.$)|(^\\?$)|(^!$)|(^,$)|(^:$)|(^;$)|(\\p{Pi})|(\\p{Pf})");
        Matcher m = p.matcher(str);
        if (m.find()) {
            System.out.println(m.group());
            return true;
        }
        return false;
    }
}
